﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.UI;
using Silvermoon.Windows.Styles;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using Silvermoon.Images;
using Silvermoon.OpenGL;
using Silvermoon.Classes;

namespace Silvermoon.Controls
{
    public class TimePickerDialog : Window
    {
        private Picker hourPicker, minutePicker;
        private TimePickerModel model;
        private AnimationGrid grid;
        private ToolbarButton saveButton;


        public TimePickerDialog()
            : base()
        {
            InitializePage();
        }

        public bool IsSaveEnabled
        {
            get { return saveButton.Enabled; }
            set { saveButton.Enabled = value; }
        }

        public TimeSpan Time
        {
            get { return GetTimeFromPicker(); }
            set
            {
                SetPickerTime(value);
            }
        }

        private void SetPickerTime(TimeSpan value)
        {
            hourPicker.SelectedItemIndex = value.Hours;
            minutePicker.SelectedItemIndex = value.Minutes;
        }

        private TimeSpan GetTimeFromPicker()
        {
            int h = hourPicker.SelectedItemIndex;
            int m = minutePicker.SelectedItemIndex;
            return new TimeSpan(h, m, 0);
        }

        private void InitializePage()
        {
            SetTransition(300, TransitionMask.Fade | TransitionMask.Zoom, TransitionMask.Fade | TransitionMask.Explode);
            Title = "CHOOSE TIME";

            const PickerMode pm = PickerMode.Fade | PickerMode.AutoHide;
            Styles.SetClass<Picker>(new PickerStyle
            {
                ItemHeight = 140,
                Lense = Shape.Empty,
                IsCyclic = true,
                Mode = pm,
                Margin = Margin.Zero,
                TextColor = PhoneColors.White
            });

            hourPicker = new Picker { Margin = Margin.Zero };
            minutePicker = new Picker { Margin = Margin.Zero };
            model = new TimePickerModel();

            grid = new AnimationGrid { Margin = Margin.Zero };

            ArrangeGrid();
            Controls.Add(grid);

            minutePicker.ItemsSource = model.Minutes;
            hourPicker.ItemsSource = model.Hours;

            Toolbar tb = new Toolbar();

            saveButton = tb.AddButton(SysImages.Ok);
            tb.AddButton(SysImages.Cancel).Tap += (s, e) => GoBack();
            saveButton.Tap += new EventHandler<TapEventArgs>(OnOk);
            Toolbar = tb;

        }

        void OnOk(object sender, TapEventArgs e)
        {
            OnSelected(Time);
        }

        protected virtual void OnSelected(TimeSpan time)
        {
            if (Selected != null) Selected(this, new TimeSpanEventArgs(time));
            GoBack();
        }

        protected override void OnShown()
        {
            base.OnShown();
            minutePicker.SelectedItemChanged += new EventHandler(OnSelectionChanged);
            hourPicker.SelectedItemChanged += new EventHandler(OnSelectionChanged);
        }

        void OnSelectionChanged(object sender, EventArgs e)
        {
            IsSaveEnabled = true;
        }

        private void ArrangeGrid()
        {
            GridDefinition def = new GridDefinition();
            def.Columns.AddRange(1f, 2f, 0.3f, 2f, 1f);
            def.Rows.AddRange(1f);
            def.CreateAutoCells();

            grid.Controls.Clear();
            grid.Definition = def;
            grid.AddControl(1, hourPicker);
            grid.AddControl(3, minutePicker);
            grid.AddControl(2, new Label { Text = ":", Font = SystemFonts.Medium, Margin = Margin.Centered, AutoSize = true });

            minutePicker.GetVirtualItem += OnGetPickerItem;
            hourPicker.GetVirtualItem += OnGetPickerItem;

        }

        void OnGetPickerItem(object sender, PickerItemEventArgs e)
        {
            var item = DatePickerItem.GetItem();
            item.Alignment = Alignment.Center;
            item.DataValue = e.Value;
            e.Container = item;
        }


        protected override void OnBack(HandledEventArgs e)
        {
            base.OnBack(e);
            Hide();
        }

        protected override void OnHidden()
        {
            base.OnHidden();
            Dispose();
        }

        public event EventHandler<TimeSpanEventArgs> Selected;


    }
}
